package com.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.Servlet;
import javax.servlet.ServletRequest;

import org.apache.catalina.connector.Request;
import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;

import com.core.baseDao.extend.Pager;
import com.manager.ClassifyManager;
import com.manager.ImgManager;
import com.manager.KeywordManager;
import com.model.Classify;
import com.model.Img;
import com.model.Keyword;
import com.model.ModuleConstant;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.util.TimeUtil;

@ParentPackage("json-default")
@Results({ @Result(name = "jsonResult", type = "json") })
@Namespace("/user")
public class ImgAction extends ActionSupport {

	@Autowired
	private KeywordManager keywordManager;
	@Autowired
	private ImgManager imgManager;
	@Autowired
	private ClassifyManager cManager;

	private Keyword keyword;
	private Img img;

	public KeywordManager getKeyManager() {
		return keywordManager;
	}

	public void setKeyManager(KeywordManager keywordManager) {
		this.keywordManager = keywordManager;
	}

	public ImgManager getImgManager() {
		return imgManager;
	}

	public void setImgManager(ImgManager imgManager) {
		this.imgManager = imgManager;
	}

	public Keyword getKeyword() {
		return keyword;
	}

	public void setKeyword(Keyword keyword) {
		this.keyword = keyword;
	}

	public Img getImg() {
		return img;
	}

	public void setImg(Img img) {
		this.img = img;
	}

	public String add() {
		Map session = ActionContext.getContext().getSession();
		List<Classify> clist = cManager.find("from Classify c ");// where
										// c.token=?",new Object[]{session.get("token")});
		ServletActionContext.getRequest().setAttribute("clist", clist);
		return "add";
	}
	
	public String addTo() {
		String ret = "";
		// Classify c = cManager.get(img.getClassify().getId());
		// img.setClassify(c);
		if(keyword==null)return "add";
		if (keyword.getId() == null) {
			img.setCreateTime(TimeUtil.getDate());
			keyword.setModel(ModuleConstant.IMG);// 设定模型
			keyword.setImg(img);
			img.setKeyword(keyword);
			img.setClick(0);
			img.setUpdateTime(TimeUtil.getDate());
			keywordManager.saveOrUpdate(keyword);
			ret = "关键字添加成功！";
		} else {
			Keyword key = (Keyword) keywordManager.findUnique(
					"from Keyword k where k.id=?",
					new Object[] {keyword.getId()});
			//Img paramImg = keyword.getImg();
			//paramImg.setClassify(key.getImg().getClassify());
			keyword.getImg().setClick(key.getImg().getClick());
			keyword.getImg().setCreateTime(key.getImg().getCreateTime());
			keyword.setModel(key.getModel());
			keyword.setToken(key.getToken());
			keyword.getImg().setUpdateTime(TimeUtil.getDate());
			keyword.getImg().setKeyword(keyword);
			// imgManager.saveOrUpdate(img);
			// keyword.setImg(img);
			keywordManager.update(keyword);
			ret = "关键字更新成功！";
		}
		ServletActionContext.getRequest().setAttribute("ret", ret);
 		return "add";
	}

	public String index() {
		ServletRequest request = ServletActionContext.getRequest();
		Pager page = new Pager();
		String p = request.getParameter("p");
		if (!StringUtils.isEmpty(p)) {
			page.setCurrPage(Integer.parseInt(p));
		}
		// Map session = ActionContext.getContext().getSession();
		// String token = (String)session.get("token");
		Map<String, Object> params = new HashMap<String, Object>();
		// params.put("token", token);
		params.put("model", ModuleConstant.IMG);
		page = keywordManager.findByPage("from Keyword ", params, page);
		request.setAttribute("page", page);
		return "index";
	}

	public String delete() {
		ServletRequest request = ServletActionContext.getRequest();
		String id = request.getParameter("id");
		Keyword key = (Keyword) keywordManager.findUnique(
				"from Keyword k where k.id=?",
				new Object[] { Integer.parseInt(id) });
		keywordManager.delete(key);
		return index();
	}

	public String edit() {
		ServletRequest request = ServletActionContext.getRequest();
		List<Classify> clist = cManager.find("from Classify c ");// where
																	// c.token=?",new Object[]{session.get("token")});
		request.setAttribute("clist", clist);
		String id = request.getParameter("id");
		Keyword key = (Keyword) keywordManager.findUnique(
				"from Keyword k where k.id=?",
				new Object[] { Integer.parseInt(id) });
		request.setAttribute("keyword", key);
		return "edit";
	}
}
